package org.wzy.xmlg_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wzy.xmlg_backend.dto.AnnouncementDTO;
import org.wzy.xmlg_backend.entity.Announcement;
import org.wzy.xmlg_backend.entity.SysUser;
import org.wzy.xmlg_backend.mapper.AnnouncementMapper;
import org.wzy.xmlg_backend.mapper.SysUserMapper;
import org.wzy.xmlg_backend.service.IAnnouncementService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {
    
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Override
    public boolean publishAnnouncement(Announcement announcement, Integer publisherId) {
        announcement.setStatus(1); // 已发布
        announcement.setPublisherId(publisherId);
        return this.save(announcement);
    }
    
    @Override
    public boolean saveDraft(Announcement announcement, Integer publisherId) {
        announcement.setStatus(0); // 草稿
        announcement.setPublisherId(publisherId);
        return this.save(announcement);
    }
    
    @Override
    public List<Announcement> getAnnouncementList() {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }
    
    @Override
    public List<Announcement> getAnnouncementsByPublisher(Integer publisherId) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher_id", publisherId)
                   .orderByDesc("create_time");
        return this.list(queryWrapper);
    }
    
    @Override
    public List<AnnouncementDTO> getAnnouncementDTOList() {
        List<Announcement> announcements = getAnnouncementList();
        return announcements.stream().map(this::convertToDTO).collect(Collectors.toList());
    }
    
    @Override
    public List<AnnouncementDTO> getAnnouncementDTOsByPublisher(Integer publisherId) {
        List<Announcement> announcements = getAnnouncementsByPublisher(publisherId);
        return announcements.stream().map(this::convertToDTO).collect(Collectors.toList());
    }
    
    @Override
    public AnnouncementDTO getAnnouncementDTOById(Integer id) {
        Announcement announcement = this.getById(id);
        return announcement != null ? convertToDTO(announcement) : null;
    }
    
    /**
     * 将Announcement转换为AnnouncementDTO
     */
    private AnnouncementDTO convertToDTO(Announcement announcement) {
        AnnouncementDTO dto = new AnnouncementDTO();
        dto.setId(announcement.getId());
        dto.setTitle(announcement.getTitle());
        dto.setContent(announcement.getContent());
        dto.setStatus(announcement.getStatus());
        dto.setPublisherId(announcement.getPublisherId());
        dto.setCreateTime(announcement.getCreateTime());
        dto.setUpdateTime(announcement.getUpdateTime());
        
        // 获取发布人姓名
        if (announcement.getPublisherId() != null) {
            SysUser user = sysUserMapper.selectById(announcement.getPublisherId());
            if (user != null) {
                dto.setPublisherName(user.getNickname() != null ? user.getNickname() : user.getUsername());
            }
        }
        
        return dto;
    }
    
    @Override
    public boolean updateAnnouncement(Announcement announcement) {
        return this.updateById(announcement);
    }
    
    @Override
    public boolean deleteAnnouncement(Integer id) {
        return this.removeById(id);
    }
}